<?php
class WebinfoAction extends CommonAction {
	public function add() {
		$data ['option_name'] = 'smtp_user';
		$data ['option_value'] = '1412128697@qq.com';
		D ( 'Options' )->data ( $data )->add ();
	}
	public function index() {
		
		// $configs=$this->get__config();
		$this->assign ( 'users_can_register', C ( 'users_can_register' ) );
		$this->display ();
	}
	public function saveConfig() {
		$options = D ( 'Options' );
		
		foreach ( $_POST as $name => $value ) {
			unset ( $data ['option_id'] ); // 删除上次保存配置时产生的option_id，否则无法插入下一条数据
			$data ['option_name'] = $name;
			$data ['option_value'] = $value;
			
			$find = $options->where ( array (
					'option_name' => $name 
			) )->select ();
			if (! $find) {
				$options->data ( $data )->add ();
			} else {
				$data ['option_id'] = $find [0] ['option_id'];
				$options->save ( $data );
			}
		}
	}
	public function handle() {
		$this->saveConfig ();
		
		$this->success ( '配置成功', 'index' );
	}
	public function setEmailConfig() {
		$this->assign ( 'send_mail', C ( 'send_mail' ) );
		$this->display ();
	}
	public function setEmailConfigHandle() {
		$this->saveConfig ();
		
		$this->success ( '配置成功', 'setEmailConfig' );
	}
	public function setSafeConfig() {
		$this->assign ( 'think_token', C ( 'think_token' ) );
		$this->assign ( 'db_fieldtype_check', C ( 'db_fieldtype_check' ) );
		$this->display ();
	}
	public function setSafeConfigHandle() {
		$this->saveConfig ();

		if($_POST['think_token']) {
			C('TOKEN_ON', true);
		}
		else {
			C('TOKEN_ON', false);
		}
		if($_POST['DB_FIELDTYPE_CHECK']) {
			C('DB_FIELDTYPE_CHECK', true);
		}
		else {
			C('DB_FIELDTYPE_CHECK', false);
		}
		
		$this->success ( '配置成功', 'setSafeConfig' );
	}
	public function links() {
		$this->linklist = D ( 'Links' )->get_links_list ( 1000 );
		
		$this->display ();
	}
	
	public function addlink() {
		if (IS_POST) {
			
			if (D ( 'Links' )->add_link ( $_POST )) {
				$this->success ( '链接添加成功',U('Admin/Webinfo/links') ); 
			} else {
				$this->error ( '链接添加失败',U('Admin/Webinfo/links') ); 
			}
		} else {
			$this->form_url=U('Admin/Webinfo/addlink');
			$this->action = '添加链接';
			$this->buttom='添加';
			$this->display ( 'addlink' );
		}
	}
	public function editlink($id) {
		if (IS_POST) {
		
			if (D ( 'Links' )->where(array('link_id'=>$id))->save ( $_POST )) {
				$this->success ( '链接编辑成功',U('Admin/Webinfo/links') );
			} else {
				$this->error ( '链接编辑失败',U('Admin/Webinfo/links') );
			}
			
		} else {
			
			$this->form_url=U('Admin/Webinfo/editlink',array('id'=>$id));
			$this->link = D ( 'Links' )->detail ( $id );
			//print_array($this->link);
			$this->action = '编辑链接';
			$this->buttom='编辑';
			$this->display ( 'addlink' );
		}
	}
	public function dellink($id) {
		if (D ( 'Links' )->del ( $id )) {
			$this->success ( '链接删除成功' );
		} else {
			$this->error ( '链接删除失败' );
		}
	}
	public function update() {
		$this->error ( '尚不支持' );
	}
	public function plugin() {
		$this->error ( '尚不支持'  );
	}
	public function info() {
		
		
		if (function_exists('gd_info')) {
			$gd = gd_info();
			$gd = $gd['GD Version'];
		} else {
			$gd = "不支持";
		}
		$info = array(
				'操作系统' => PHP_OS,
				'主机名IP端口' => $_SERVER['SERVER_NAME'] . ' (' . $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT'] . ')',
				'运行环境' => $_SERVER["SERVER_SOFTWARE"],
				'PHP运行方式' => php_sapi_name(),
				'程序目录' => WEB_ROOT,
				'MYSQL版本' => function_exists("mysql_close") ? mysql_get_client_info() : '不支持',
				'GD库版本' => $gd,
				//            'MYSQL版本' => mysql_get_server_info(),
				'上传附件限制' => ini_get('upload_max_filesize'),
				'执行时间限制' => ini_get('max_execution_time') . "秒",
				'剩余空间' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
				'服务器时间' => date("Y年n月j日 H:i:s"),
				'北京时间' => gmdate("Y年n月j日 H:i:s", time() + 8 * 3600),
				'采集函数检测' => ini_get('allow_url_fopen') ? '支持' : '不支持',
				'register_globals' => get_cfg_var("register_globals") == "1" ? "ON" : "OFF",
				'magic_quotes_gpc' => (1 === get_magic_quotes_gpc()) ? 'YES' : 'NO',
				'magic_quotes_runtime' => (1 === get_magic_quotes_runtime()) ? 'YES' : 'NO',
		);
		$this->assign('server_info', $info);
		
		
		
		$this->display ( 'info' );
	}
}
